/**
 * 219. 存在重复元素 II
 */
class Solution1 {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        int[] ans = new int[2];
        for(int i = 0;i<nums.length;i++){
            //两个索引的差值的绝对值超过k的值就不需要去遍历
            for(int j = i+1;j<nums.length&&Math.abs(i-j) <= k;j++){
                if(nums[i] == nums[j]){
                    
                        return true;
                    
                }
            }
        }
        return false;
    }
}

/**
 * 258. 各位相加
 */
class Solution2 {
    public int addDigits(int num) {
        if (num < 10){
            return num;
        }
        int ans = 0;
        while(num >= 10){
            ans = 0;
            while(num > 0){
                ans += num%10;
                num /= 10;
            }
            num = ans;
        }

        return ans;
    }
}

/**
 * 283. 移动零
 */
class Solution3 {
    public void moveZeroes(int[] nums) {
        int j = 0;
        for(int i = 0;i<nums.length;i++){
            if(nums[i] != 0){
                nums[j++] = nums[i];
            }
        }
        while(j < nums.length){
            nums[j++] = 0;
        }

    }
}

class Solution4 {
    public void moveZeroes(int[] nums) {
        int a = 0;
        int b = 0;
        while(a < nums.length && b < nums.length){
            if(nums[a] != 0)
                nums[b++] = nums[a];
            a++;
        }
        while( b < nums.length){
            nums[b++] = 0;
        }

    }
}